    class HotelController < ApplicationController

 def index
    session[:index_page] = params[:page] == nil ? nil : params[:page]
    @filterrific = initialize_filterrific(HotelMaster,params[:filterrific]) or return
    @hotels = @filterrific.find.where("sections_id=?",session[:section_id]).page(params[:page]).order(created_at: :desc)
 end

 def edit
    id=params[:id]
    session[:index_page] = params[:page] == nil ? nil : params[:page]
    @filterrific = initialize_filterrific(HotelInfo,params[:filterrific]) or return
    @hotels_info = @filterrific.find.where("master_id=#{id}").page(params[:page]).order(created_at: :desc)
    @mater_id=id
 end
 
 def info_del
    hotels = HotelInfo.find(params[:id])
    hotels.destroy
    notice='删除成功'  
    sum=HotelInfo.all
    per=HotelInfo.per_page.to_i
    session[:index_page]=(session[:index_page].to_i - 1).to_s  if sum.size % per ==0 && session[:index_page].to_i >1
    redirect_to edit_hotel_path(id:hotels.master_id,page: session[:index_page]), notice: notice
 end

 def update_edit
     name=params["hotel"]["name"]
     id=params["hotel"]["id"]
     ref=HotelMaster.find_by_sql("SELECT * FROM hotel_master where name='#{name}' and id<>#{id} ").size
     if ref>0
         notice= "酒店名称已存在!"
     else
        aa=HotelMaster.find(id)
        aa.name=name
        aa.created_at=Time.now().strftime("%Y-%m-%d %H:%M:%S")
        aa.save
        notice= "修改成功!"
     end
     redirect_to  hotel_index_path ,notice: notice
 end
 
 def update_info_edit    
     name=params["hotel"]["name"]
     id=params["hotel"]["hotel_id"]
     address=params["hotel"]["address"]
     master_id=params["hotel"]["master_id"]
     ref=HotelInfo.find_by_sql("SELECT * FROM hotel_info where name='#{name}' and id<>#{id} and master_id=#{master_id} ").size
     if ref>0
         notice= "酒店名称已存在!"
     else
        aa=HotelInfo.find(id)
        aa.name=name
        aa.address=address
        aa.created_at=Time.now().strftime("%Y-%m-%d %H:%M:%S")
        aa.save
        notice= "修改成功!"
     end
     redirect_to edit_hotel_path(id:master_id), notice: notice
 end


def hotel_edit_ajax   #ajax 调用
      a=params[:a]
      id=params[:id]
      b=params[:b]
      if id==0   #新增
         ref=HotelInfo.find_by_sql("SELECT * FROM hotel_info where name='#{a}' and master_id=#{b}  ").size
      else     #修改
         ref=HotelInfo.find_by_sql("SELECT * FROM hotel_info where name='#{a}' and id<>#{id} and master_id=#{b} ").size
      end
      if ref>0
         ret='0'    #名称已经存在
      else
         ret='1'
      end
      render text:ret
 end

 def update_info_new
     name=params["hotel"]["name"]
     master_id=params["hotel"]["master_id"]
     address=params["hotel"]["address"]
     ref=HotelInfo.find_by_sql("SELECT * FROM hotel_info where name='#{name}' and master_id=#{master_id}   ").size
     if ref>0
         notice= "酒店名称已存在!"
     else
        aa=HotelInfo.new
        aa.name=name
        aa.master_id=master_id
        aa.address=address
        aa.sections_id=session[:section_id]   #添加组织机构
        aa.created_at=Time.now().strftime("%Y-%m-%d %H:%M:%S")
        aa.save
        notice= "新增成功!"
     end
     redirect_to edit_hotel_path(id:master_id), notice: notice
 end



 def update_new
     name=params["hotel"]["name"]
     ref=HotelMaster.find_by_sql("SELECT * FROM hotel_master where name='#{name}'  ").size
     if ref>0
         notice= "酒店名称已存在!"
     else
        aa=HotelMaster.new
        aa.name=name
        aa.sections_id=session[:section_id]
        aa.created_at=Time.now().strftime("%Y-%m-%d %H:%M:%S")
        aa.save
        notice= "新增成功!"
     end
     redirect_to  hotel_index_path ,notice: notice
 end

 def destroy
     hotels = HotelMaster.find(params[:id])
     ref=HotelMaster.find_by_sql(" SELECT b.id FROM hotel_info b  WHERE b.master_id=#{hotels.id} ").size
     if ref>0 
        notice= "请先删除该酒店地址!"
     else
        hotels.destroy
        notice='删除成功'  
        sum=HotelMaster.all
        per=HotelMaster.per_page.to_i
        session[:index_page]=(session[:index_page].to_i - 1).to_s  if sum.size % per ==0 && session[:index_page].to_i >1
     end
      redirect_to  hotel_index_path(page: session[:index_page]), notice: notice
 end
 
 def hotel_ajax   #ajax 调用
      a=params[:a]
      id=params[:id]
      if id==0   #新增
         ref=HotelMaster.find_by_sql("SELECT * FROM hotel_master where name='#{a}'  ").size
      else     #修改
         ref=HotelMaster.find_by_sql("SELECT * FROM hotel_master where name='#{a}' and id<>#{id}  ").size
      end
      if ref>0
         ret='0'    #名称已经存在
      else
         ret='1'
      end
      render text:ret
 end
 def hotel_ajax_edit_load 
     a=params[:a]
     hotels = HotelMaster.find(a).to_json.to_s
     render text:hotels
 end
 def hotel_ajax_edit_info_load 
     a=params[:a]
     hotels = HotelInfo.find(a).to_json.to_s
     render text:hotels
 end

end
